package com.itheima.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.common.R;
import com.itheima.admin.pojo.OrderDto;
import com.itheima.service.OrderService;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @Author longbosheng
 * @Date 2022/6/18 20:35
 * @PackageName:com.itheima.controller
 * @ClassName: OrderController
 * @Description: TODO
 * @Version 1.0
 */
@Log4j2
@RestController
@RequestMapping("/order")
public class OrderController {
    @Autowired
    private OrderService orderService;

    /**
     * 修改订单状态
     *
     * @param order
     * @return
     */
    @PutMapping
    public R updateOrder(@RequestBody OrderDto order) {
        UpdateWrapper<OrderDto> wrapper = new UpdateWrapper<>();
        wrapper.eq("id", order.getId());
        wrapper.set("status", order.getStatus());
        boolean update = orderService.update(wrapper);
        if (update) {
            return R.success("修改成功");
        } else {
            return R.error("修改失败");
        }
    }

    /**
     * 订单分页查询
     *
     * @return
     */
    @GetMapping("/page")
    public R selectOrder(String beginTime, String endTime, String number, int page, int pageSize) {
        QueryWrapper<OrderDto> wrapper = new QueryWrapper<>();
        IPage<OrderDto> page1 = new Page<>(page, pageSize);

//        wrapper.ge(beginTime != null, "order_time", beginTime);
//        wrapper.le(endTime != null, "order_time", endTime);
        wrapper.between(beginTime != null || endTime != null, "order_time", beginTime, endTime);
        wrapper.eq(number != null, "number", number);

        IPage<OrderDto> orderDtoIPage = orderService.page(page1, wrapper);
        log.debug(orderDtoIPage.getRecords());
        return R.success(orderDtoIPage);
    }
}
